//
// Created on 2025/10/31.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef OHOS_RIVE_FONT_HELPER_H
#define OHOS_RIVE_FONT_HELPER_H

#include "general.h"
#include "napi/native_api.h"
#include "rive/text/font_hb.hpp"
#include <arkui/native_node_napi.h>
#include <mutex>
#include <unordered_map>
#include <vector>

namespace ohos_rive {
class FontHelper {
private:
    static std::unordered_map<uint16_t, std::vector<rive::rcp<rive::Font>>> s_pickFontCache;
    static std::mutex s_fallbackFontsMutex;

    static const std::vector<rive::rcp<rive::Font>> &pick_fonts(const uint16_t weight);

    static napi_env g_env;

public:
    static std::vector<rive::rcp<rive::Font>> s_fallbackFonts;

    static void resetCache()
    {
        std::lock_guard<std::mutex> lock(FontHelper::s_fallbackFontsMutex);
        FontHelper::s_pickFontCache.clear();
    }

    static bool RegisterFallbackFont(napi_env env, napi_value array);

    static std::vector<uint8_t> GetSystemFontBytes();

    static rive::rcp<rive::Font> FindFontFallback(const rive::Unichar missing,
                                                  const uint32_t fallbackIndex,
                                                  const rive::Font *riveFont);
};
} // namespace ohos_rive

#endif // OHOS_RIVE_FONT_HELPER_H
